import { AuthRouteWhites } from '@/router'
import ls from '@/lib/ls'
import Api from '@/api'
import router from '@/router/index.js'
import useConfigStore from '@/store/config'
import { closeWebSocket } from '@/utils/websocket' 

export default {

    // 启动应用
    async Start(route) {
        //当前页面在认证白名单内
        if (AuthRouteWhites.includes(route.path)) {
            this.completed = true
            return
        }
        const token = ls.get('TOKEN')
        try {
            //本地有token存在
            if (!!token === true) {
                await this.Init()
            } else {
                // 设备断开  
                const configStore = useConfigStore()
                closeWebSocket() 
                configStore.disconnect() 
                ls.clear()
                ElNotification({
                    title: '提示',
                    message: 'token过期,请重新登录',
                    type: 'error',
                })   
                throw new Error('没有token')
            }
        } catch (err) {
            console.log(err)
            ls.remove('TOKEN')
        }
    },
    // 初始化应用
    async Init(type) {
        const configStore = useConfigStore()
        // try {
        // } catch (error) {
        //     console.log('error init')
        // }
        if(type == 'login'){
            await configStore.UpdateAll()
        }else{
            if(!configStore.shopInfo || !configStore.shopInfo.orgCode){
                await configStore.UpdateAll()
            }
        }
        // await configStore.UpdateAll()
        const resp = {
            user: {
                username: '赵明良'
            }
        }
        const { user } = resp
        this.user = user
        this.completed = true
    },
    // // 登录
    // Login(){
    //     // 登录换取凭证
    //     const resp = {
    //         token: '111',
    //         refreshToken: '222',
    //     }
    //     ls.set('TOKEN',resp.token)
    //     ls.set('RT',resp.refreshToken)
    //     this.completed = false
    // 	return this.Init()
    // },
    // 登录
    async Login(form) {
        // console.log('登录参数:', form);
        // 登录换取凭证
        const resp = await Api.Account.Login.Do(form)
        // console.log('登录获取用户基础信息:', resp);
        ls.set('TOKEN', resp.token)
        ls.set('USER_INFO', resp)

        // 注意path 要根据实际router同步配置
        const asideLocalMenuList = [
            {
                name: 'consult', // consult
                title: '咨客',
                path: '/consult'
            },
            {
                name: 'queue', // queue
                title: '排队',
                path: '/queue'
            },
            {
                name: 'order', // order
                title: '点单',
                path: '/order'
            },
            {
                name: 'member', // member
                title: '会员',
                path: '/member'
            },
            {
                name: 'product', // product
                title: '商品',
                path: '/product'
            },
            {
                name: 'onorder', // onorder
                title: '订单',
                path: '/onorder'
            },
            {
                name: 'wine', // wine
                title: '存取酒',
                path: '/wine'
            },
            {
                name: 'cashier', // cashier
                title: '更多',
                path: '', //此处特殊 直接弹窗不是菜单 
            }
        ]
        // 获取用户权限信息
        const resMenu = await Api.Common.PcObjProcess.Do({ "functionName": "GetUserMenuButton", "contentData": { obj: "" } })
        console.log(resMenu);
        
        const asideMap = new Map(resMenu.result.map(item => [item.name, item]));
        const routerList = asideLocalMenuList.filter(item => asideMap.has(item.name))
        console.log('routerList:', routerList);
        // 还未处理只有一个菜单为更多的情况
        if (routerList.length > 0) {
            ls.set('USER_ROLE_MENU', routerList)
            ls.set('USER_ROLE_ALL', resMenu.result)
            this.completed = false
            this.Init('login')
            return routerList[0].path
        } else {
            ElNotification({ title: '提示', message: '请联系管理员配置菜单权限', type: 'error' })
            ls.clear()
            return ''
        }
    },
    // 退出登录
    Logout() {
        ls.clear()
        router.replace({
            path: "/login",
          });
    },
}